<template>
  <!-- 所属公交车辆 -->
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="船舶名称" prop="shipId">
        <el-select v-model="queryParams.shipId" clearable placeholder="请选择所属线路">
          <el-option
            v-for="dict in busLineList"
            :key="dict.shipId"
            :label="dict.shipName"
            :value="dict.shipId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bhxq:busInfo:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新建企业舰船底册</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="shippingList">
      <el-table-column label="企业名称" align="center" prop="orgName" />
      <el-table-column label="船舶名称" align="center" prop="shipName" />
      <el-table-column label="船舶种类" align="center" prop="shipType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bhxq_ship_type" :value="scope.row.shipType" />
        </template>
      </el-table-column>
      <el-table-column label="船籍港名称" align="center" prop="portName" />
      <el-table-column label="船舶长度" align="center" prop="shipLength" />
      <el-table-column label="船舶型宽" align="center" prop="shipWidth" />
      <el-table-column label="船舶总吨位" align="center" prop="shipTotalTonnage" />
      <el-table-column label="船舶净吨位" align="center" prop="shipNetTonnage" />
      <el-table-column label="船舶建成日期" align="center" prop="completedDate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['bhxq:busInfo:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['bhxq:busInfo:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公交站点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="船舶名称" prop="shipName">
              <el-input v-model="form.shipName" placeholder="请输入船舶名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="航行区域" prop="navigateArea">
              <el-select v-model="form.navigateArea" placeholder="请选择航行区域" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.bhxq_ship_navigate_area"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="航行营业状态" prop="operationStatus">
              <el-select v-model="form.operationStatus" placeholder="请选择航行营业状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.bhxq_ship_operation_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="船舶种类" prop="shipType">
              <el-select v-model="form.shipType" placeholder="请选择船舶种类" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.bhxq_ship_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="载货单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入载货单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="承载数量" prop="quota">
              <el-input class="delInput" v-model="form.quota" type="number" placeholder="请输入承载数量"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="船籍港名称" prop="portName">
              <el-input v-model="form.portName" placeholder="请输入船籍港名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="船舶长度" prop="shipLength">
              <el-input class="delInput" v-model.number="form.shipLength" type="number" placeholder="请输入船舶长度"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="船舶型宽" prop="shipWidth">
              <el-input class="delInput" v-model.number="form.shipWidth" type="number" placeholder="请输入船舶型宽"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="船舶总吨位" prop="shipTotalTonnage">
              <el-input class="delInput" v-model.number="form.shipTotalTonnage" type="number" placeholder="请输入船舶总吨位"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="船舶建成日期" prop="completedDate">
              <el-date-picker
                v-model="form.completedDate"
                clearable
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择船舶建成日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="船舶登记号" prop="shipRegisterNum">
              <el-input class="delInput" v-model.number="form.shipRegisterNum" type="number" placeholder="请输入船舶登记号"></el-input>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="船检登记号" prop="shipCheckRegisterNum">
              <el-input class="delInput" v-model.number="form.shipCheckRegisterNum" type="number" placeholder="请输入船检登记号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="船舶净吨位" prop="shipNetTonnage">
              <el-input class="delInput" v-model.number="form.shipNetTonnage" type="number" placeholder="请输入船舶净吨位"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="title !== '修改舰船底册'" type="primary" @click="submitForm('ContinueFill')">确定并继续填写</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addShipping, listShipping, getSHipping, delShipp, updateShipp, getAllShipp } from '@/api/informationReport'

export default {
  name: 'Index',
  dicts: ['bhxq_ship_navigate_area', 'bhxq_ship_type', 'bhxq_ship_operation_status'],
  data() {
    return {
      form: {},
      title: '',
      open: false,
      total: 0,
      loading: true,
      // 航空区域字典
      aviaAreaChange: [],
      // 航行营业状态字典
      navigaStatus: [
        {
          label: '营业',
          value: '1'
        },
        {
          label: '报停',
          value: '2'
        },
        {
          label: '注销',
          value: '3'
        },
        {
          label: '吊销',
          value: '4'
        }
      ],
      // 船舶种类字典
      shiptypeList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10

      },
      shippingList: [], // 船舶列表数据
      busLineList: [], // 所有船舶信息
      rules: {
        shipName: [
          { required: true, message: '船舶名称不能为空', trigger: 'blur' }
        ],
        navigateArea: [
          { required: true, message: '航行区域不能为空', trigger: 'change' }
        ],
        operationStatus: [
          { required: true, message: '航行营业状态不能为空', trigger: 'change' }
        ],
        shipType: [
          { required: true, message: '船舶种类不能为空', trigger: 'change' }
        ],
        unit: [
          { required: true, message: '载货单位不能为空', trigger: 'blur' }
        ],
        quota: [
          { required: true, message: '承载数量不能为空', trigger: 'blur' }
        ],
        portName: [
          { required: true, message: '船籍港名称不能为空', trigger: 'blur' }
        ],
        shipLength: [
          { required: true, message: '船舶长度不能为空', trigger: 'blur' }
        ],
        shipWidth: [
          { required: true, message: '船舶型宽不能为空', trigger: 'blur' }
        ],
        shipTotalTonnage: [
          { required: true, message: '船舶总吨位不能为空', trigger: 'blur' }
        ],
        completedDate: [
          { required: true, message: '船舶建成日期不能为空', trigger: 'blur' }
        ],
        shipRegisterNum: [
          { required: true, message: '船舶登记号不能为空', trigger: 'blur' }
        ],
        shipCheckRegisterNum: [
          { required: true, message: '船检登记号不能为空', trigger: 'blur' }
        ],
        shipNetTonnage: [
          { required: true, message: '船舶净吨位不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getAllShipList()
    this.getList()
  },
  methods: {
    /** 获取所有船舶名称 */
    getAllShipList() {
      getAllShipp().then(response => {
        this.busLineList = response.data
        console.log('data', response)
      })
    },
    getList() {
      this.loading = true
      listShipping(this.queryParams).then(response => {
        this.shippingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增舰船底册'
    },
    /** 提交按钮 */
    submitForm(type) {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.shipId != null) {
            updateShipp(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addShipping(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.getAllShipList()
              // 当点击确认确定并继续填写时 模态框不关闭
              if (type === 'ContinueFill') {
                this.reset()
                this.open = true
              } else {
                this.open = false
              }
              this.getList()
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset()
      // const id = row.id || this.ids
      getSHipping(row.shipId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改舰船底册'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除船舶名称为"' + row.shipName + '"的数据项？').then(function() {
        return delShipp(row.shipId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        shipName: '',
        navigateArea: '',
        operationStatus: '',
        shipType: '',
        unit: '',
        quota: '',
        portName: '',
        shipLength: '',
        shipWidth: '',
        shipTotalTonnage: '',
        completedDate: '',
        shipRegisterNum: '',
        shipCheckRegisterNum: '',
        shipNetTonnage: ''
      }
      this.resetForm('form')
    }
  }
}
</script>

<style scoped>
/deep/.delInput input::-webkit-outer-spin-button,
/deep/.delInput input::-webkit-inner-spin-button {
  -webkit-appearance: none!important;
}
/deep/.delInput input[type="number"]{
  -moz-appearance: textfield;
}
/deep/.delInput .el-input__inner{
  line-height: 1px !important;
}
</style>
